package com.xinya.wanandroidclient.http


/**
 *
 * @ClassName:      RetrofitManager$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/2/24$ 14:25$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class RetrofitManager {
    companion object {
        /**
         * 存储ApiService
         */
        private val map = mutableMapOf<Class<*>, Any>()

        /**
         * 获取wan android的retrofit
         */
        private val wanAndroidRetrofit = RetrofitFactory.factory()

        //动态指定域名
        fun <T : Any> getApiService(apiClass: Class<T>): T {
            return getService(apiClass)
        }

        /**
         * 获取ApiService单例对象
         */
        private fun <T : Any> getService(apiClass: Class<T>): T{
            //重入锁单例避免多线程安全问题
            return if (map[apiClass] == null) {
                synchronized(RetrofitManager::class.java) {
                    val t = wanAndroidRetrofit.create(apiClass)
                    if (map[apiClass] == null) {
                        map[apiClass] = t
                    }
                    t
                }
            } else {
                map[apiClass] as T
            }
        }
    }
}